import axios from 'axios'
import {Message} from 'element-ui'
import cookie from 'js-cookie'

axios.defaults.baseURL = process.env.NODE_ENV === 'production'
  ? 'https://api.ts57.cn/app'
  : '/api'

// ==============
axios.interceptors.request.use(config => {
  config.headers['Authorization'] = 'Bearer ' + cookie.get('x-token')
  return config
}, err => {
  return Promise.reject(err)
})
// ==============
// http response 拦截器
axios.interceptors.response.use(
  response => {
    console.log(response)
    if (response.data.code) {
    }
    return response
  }, err => {
    console.log(err.response)
  if (err.response) {
    Message({
      message: err.response.data.message  || '发生未知错误！！',
      type: 'error'
    })
  } else {
    Message({
      message: '糟糕，发生网络错误！！！',
      type: 'error'
    })
  }
  return Promise.reject(err)
})

export default axios
